﻿#include <iostream>
#include <boost/beast/http.hpp>
#include <boost/beast.hpp>
#include <boost/asio.hpp>
#include <json/json.h>
#include <json/value.h>
#include <json/reader.h>
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace http = beast::http;
namespace http = boost::beast::http;
using tcp = asio::ip::tcp;
using namespace std;
string getRandomEmail() {
    srand(123456);
    string a = "asd";
    return a + "@qq.com";
}
//测试简单的多线程发送
void sendreq1() {
    asio::io_context io_context;
    tcp::resolver resolver(io_context);
    auto endpoints = resolver.resolve("127.0.0.1/get_test", "8080");
    tcp::socket socket(io_context);
    asio::connect(socket, endpoints);

    http::request<http::string_body> req{http::verb::post, "/get_test", 11};
    //请求头参数
    req.set(http::field::host, "127.0.0.1");
    req.set(http::field::content_type, "application/json");
    req.set(http::field::keep_alive, "1,1");
    req.prepare_payload();
    
    for (int i = 0; i < 100; i++) {
        http::write(socket, req);

        // 响应:
        beast::flat_buffer buffer;
        http::response<http::string_body> res;
        http::read(socket, buffer, res);

        // 打印响应状态码和响应体
        std::cout << "Response status: " << std::string(res.reason()) << "\n";
        std::cout << "Response body: " << res.body() << "\n";
    }
}
    
//测试发送登录请求，考研后面的数据库读取，多并发能力
void sendreq2() {
    asio::io_context io_context;
    tcp::resolver resolver(io_context);
    http::request<http::dynamic_body> _request;

    auto endpoints = resolver.resolve("127.0.0.1", "8080");
    tcp::socket socket(io_context);
    asio::connect(socket, endpoints);

    http::request<http::string_body> req{http::verb::post, "/user_login", 11};
    //请求头参数
    req.set(http::field::host, "127.0.0.1");
    req.set(http::field::content_type, "application/json");
    req.set(http::field::keep_alive, "1,1");

    for (int i = 0; i < 100; i++) {
        Json::Value root;
        Json::Reader reader;

        root["email"] = getRandomEmail();
        root["pwd"] = "123456";
        string jsonobj = root.toStyledString();

        req.body() = jsonobj;
        req.prepare_payload();
        http::write(socket, req);

        // 响应:
        beast::flat_buffer buffer;
        http::response<http::string_body> res;
        http::read(socket, buffer, res);

        // 打印响应状态码和响应体
        std::cout << "Response status: " << std::string(res.reason()) << "\n";
        std::cout << "Response body: " << res.body() << "\n";
    }
}


int main()
{
    vector<thread> threads1;
    vector<thread> threads2;
    try {
        for (int i = 0; i < 10; i++) {
            threads1.emplace_back(sendreq1);
            //threads2.emplace_back(sendreq2);
        }

        for (auto& t : threads1) {
            t.join();
        }
    }
    catch (exception& e) {
        cout << "exception:" << e.what() << endl;
        for (auto& t : threads1) {
            t.join();
        }
    }
}


